<?php

class UserShopService {

    /**
     * @var UserShopService
     */
    private static $instance;

    /**
     * Get the instance of UserShopService
     * @return UserShopService
     */
    public static function get() {
        if (!self::$instance) {
            self::$instance = new UserShopService();
        }
        return self::$instance;
    }

    /**
     * Construction.
     */
    public function __construct() {

    }

    /**
     * 清理玩家商铺数据
     * @param $userId
     * @param $dateInt
     * @return bool
     * @throws SzException
     */
    public function clearShop($userId,$dateInt) {
        $day=GameConst::SHOP_SAVE_DAY;
        $delDateIntStart=$dateInt-$day*24*3600;

        /**
         * @var  $userShop  UserShopVo
         */

        //获取玩家商铺数据
        $userShopList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_SHOP);
        if ($userShopList->getListCount() !=0){
            $userShopInfo=$userShopList->toArray();
            //商铺信息超过保留的天数删除
            foreach ($userShopInfo as $key=>$data){
                if ($key < $delDateIntStart){
                    $userShopList->deleteElement($key);
                    SzPersister::get()->setVoList($userShopList);
                    unset($userShopInfo[$key]);
                }
            }
        }

    }

    /**
     * 获取商铺数据
     * @param $userId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function shopInfo($userId,$reqTime) {
        $Ymd=date('Ymd',$reqTime);
        $dateInt=strtotime($Ymd);
        $this->clearShop($userId,$dateInt);

        /**
         * @var $userShopList UserShopVoList
         */
        //获取玩家vip商店数据
        $userShopList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_SHOP);
        $userShopData = $userShopList->toArray();

        //整合返回数据
        return $userShopData;
    }

    /**
     * 店铺经营记录
     * @param $userId
     * @param $type
     * @param $itemId
     * @param $count
     * @return array
     * @throws SzException
     */
    public function record($userId,$type,$itemId,$count,$reqTime=''){
        if (!$reqTime) {
            //当前时间获取
            $reqTime = GameService::get()->getReqTime();
        }
        $Ymd=date('Ymd',$reqTime);
        $dateInt=strtotime($Ymd);
        /**
         * @var $userShopList UserShopVoList
         * @var $userShop  UserShopVo
         */

        $userShopList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_SHOP);
        $userShop = $userShopList->getElement($dateInt,false);
        if (!$userShop) {
            $userShop = new UserShopVo(
                $userId,
                $dateInt,
                $Ymd,
                0,json_encode([]),
                0,json_encode([]),
                0,json_encode([]),
                0,json_encode([]),
                0,json_encode([]),
                0,json_encode([]),
                0,json_encode([]),
                0,json_encode([]),
                0,0,0,
                $reqTime,true);
        }


        if ($type==1) {//鲜花（包括收取，出售，订单）
            if ($itemId==GameConst::SHOP_REC_FLOWER) {
                //鲜花收取
              $recFlower =  $userShop->getRecFlower();
              $recFlower+=$count;
              $userShop->setRecFlower($recFlower);
            }elseif ($itemId==GameConst::SHOP_SELL_FLOWER) {
                //花台出售
                $sellFlower =  $userShop->getSellFlower();
                $sellFlower+=$count;
                $userShop->setSellFlower($sellFlower);
            }elseif ($itemId==GameConst::SHOP_FLOWER_ORDER) {
                //鲜花订单
                $flowerOrder =  $userShop->getFlowerOrder();
                $flowerOrder +=$count;
                $userShop->setFlowerOrder($flowerOrder);
            }
        } elseif ($type==2) { //道具
            if ($itemId==GameConst::ITEM_ID_TOKEN) {//金币
                $gold= $userShop->getGold();
                $goldDetail=$userShop->getGoldDetail();
                $gold+=$count;
             /*   if (!$goldDetail) {
                    $goldDetailArr=array();
                    $goldDetailArr[$reqTime]=$count;
                } else {
                    $goldDetailArr=json_decode($goldDetail,true);
                    $goldDetailArr[$reqTime]=$count;
                }*/
                $userShop->setGold($gold);
               // $userShop->setGoldDetail(json_encode($goldDetailArr));
            } elseif ($itemId==GameConst::ITEM_ID_MONEY) { //钻石
                $gem= $userShop->getGem();
                $gemDetail=$userShop->getGemDetail();
                $gem+=$count;
              /*  if (!$gemDetail) {
                    $gemDetailArr=array();
                    $gemDetailArr[$reqTime]=$count;
                } else {
                    $gemDetailArr=json_decode($gemDetail,true);
                    $gemDetailArr[$reqTime]=$count;
                }*/
                $userShop->setGem($gem);
               // $userShop->setGemDetail(json_encode($gemDetailArr));
            } elseif ($itemId==GameConst::ITEM_ID_EXP) {   //经验
                $exp= $userShop->getExp();
                $expDetail=$userShop->getExpDetail();
                $exp+=$count;
         /*       if (!$expDetail) {
                    $expDetailArr=array();
                    $expDetailArr[$reqTime]=$count;
                } else {
                    $expDetailArr=json_decode($expDetail,true);
                    $expDetailArr[$reqTime]=$count;
                }*/
                $userShop->setExp($exp);
               // $userShop->setExpDetail(json_encode($expDetailArr));
            } elseif ($itemId==GameConst::ITEM_ID_GRANGMONTHER_TICKET) {   //好评劵
                $grandmonther= $userShop->getGrandmonther();
                $grandmontherDetail=$userShop->getGrandmontherDetail();
                $grandmonther+=$count;
                if (!$grandmontherDetail) {
                    $grandmontherDetailArr=array();
                    $grandmontherDetailArr[$reqTime]=$count;
                } else {
                    $grandmontherDetailArr=json_decode($grandmontherDetail,true);
                    $grandmontherDetailArr[$reqTime]=$count;
                }
                $userShop->setGrandmonther($grandmonther);
                $userShop->setGrandmontherDetail(json_encode($grandmontherDetailArr));
            } elseif ($itemId==GameConst::ITEM_ID_WATER) {   //水
                $water= $userShop->getWater();
                $waterDetail=$userShop->getWaterDetail();
                $water+=$count;
           /*     if (!$waterDetail) {
                    $waterDetailArr=array();
                    $waterDetailArr[$reqTime]=$count;
                } else {
                    $waterDetailArr=json_decode($waterDetail,true);
                    $waterDetailArr[$reqTime]=$count;
                }*/
                $userShop->setWater($water);
               // $userShop->setWaterDetail(json_encode($waterDetailArr));
            }
        }

        if ($_SERVER['is_payment_callback'] ?? false) $userShop->persist();
        if ($userShop->isInsert() && !SzUtility::checkArrayKey($dateInt, $userShopList->getList())) {
            $userShopList->addElement($userShop);
            $userShopList->setElement($dateInt, $userShop);
        } else {
            $userShopList->updateElement($dateInt, $userShop);
        }

        SzPersister::get()->setVoList($userShopList);

    }

}
